package com.example.controller;

import com.example.common.Result;
import com.example.entity.Address;
import com.example.entity.Clockcalendar;
import com.example.entity.Learningrange;
import com.example.service.ClockcalendarService;
import com.example.service.LearningrangeService;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.*;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 用户打卡日历前端接口
 */
@RestController
@Api(value = "学习范围", tags = {"学习范围的增删改查"})
@RequestMapping("/learningrange")
public class LearningrangeController {

    @Resource
    private LearningrangeService learningrangeService;


    /**
     * 根据uid查询用户数据，存在则更新，不存在则新增
     */
    @ApiOperation(value = "增加或者更新对应uid的用户的学习范围", notes = "返回是否成功")
    @PostMapping("/addOrUpdateByUid")
    public Result addOrUpdateByUid(@RequestBody Learningrange learningrange) {
        // 根据uid查询用户数据
        Learningrange existingLearningrange = learningrangeService.selectByUid(learningrange.getUid());

        if (existingLearningrange != null) {
            // 如果存在该uid的用户数据，则执行更新操作
//            existingLearningrange.setBid(learningrange.getBid());
            learningrangeService.updateById(learningrange);
        } else {
            // 如果不存在该uid的用户数据，则执行新增操作
            learningrangeService.add(learningrange);
        }

        return Result.success();
    }


    /**
     * 新增
     */
    @ApiOperation(value = "增加不同用户的学习范围", notes = "返回是否成功")
    @PostMapping("/add")
    public Result add(@RequestBody Learningrange learningrange) {
        learningrangeService.add(learningrange);
        return Result.success();
    }

    /**
     * 删除
     */
    @ApiOperation(value = "删除学习范围", notes = "返回是否成功")
    @ApiImplicitParams({
            @ApiImplicitParam(
                    name = "id",
                    value = "要删除的学习范围id,如'1'"
            )
    })
    @DeleteMapping("/delete/{id}")
    public Result deleteById(@PathVariable Integer id) {
        learningrangeService.deleteById(id);
        return Result.success();
    }

    /**
     * 批量删除
     */
    @ApiOperation(value = "批量删除学习范围", notes = "返回是否成功")
    @DeleteMapping("/delete/batch")
    public Result deleteBatch(@RequestBody List<Integer> ids) {
        learningrangeService.deleteBatch(ids);
        return Result.success();
    }

    /**
     * 修改
     */
    @ApiOperation(value = "更新学习范围信息", notes = "返回是否成功")
    @PutMapping("/update")
    public Result updateById(@RequestBody Learningrange learningrange) {
        learningrangeService.updateById(learningrange);
        return Result.success();
    }

    /**
     * 根据ID查询
     */
    @ApiOperation(value = "通过id查询学习范围信息", notes = "返回Models中的学习范围信息类型")
    @ApiImplicitParams({
            @ApiImplicitParam(
                    name = "id",
                    value = "要查询的学习范围id,如'1'"
            )
    })
    @ApiResponses({
            @ApiResponse(code = 200, message = "成功", response = Learningrange.class,reference = "Learningrange")
    })
    @GetMapping("/selectById/{id}")
    public Result selectById(@PathVariable Integer id) {
        Learningrange learningrange = learningrangeService.selectById(id);
        return Result.success(learningrange);
    }

    /**
     * 查询所有
     */
    @ApiOperation(value = "查询所有符合特征的学习范围信息", notes = "返回Models中的学习范围信息类型的数组")
    @ApiResponses({
            @ApiResponse(code = 200, message = "成功", response = Learningrange.class,reference = "Learningrange")
    })
    @GetMapping("/selectAll")
    public Result selectAll(Learningrange learningrange ) {
        List<Learningrange> learningrangeList= learningrangeService.selectAll(learningrange);
        return Result.success(learningrangeList);
    }

    /**
     * 分页查询
     */
    @ApiOperation(value = "分页查询所有符合特征的学习范围信息", notes = "返回Models中的学习范围信息的分页数组")
    @ApiImplicitParams({
            @ApiImplicitParam(
                    name = "pageNum",
                    value = "要查询的页码,默认为'1'"
            ),
            @ApiImplicitParam(
                    name = "pageSize",
                    value = "每页尺寸,默认为'10'"
            )
    })
    @ApiResponses({
            @ApiResponse(code = 200, message = "成功", response = Learningrange.class,reference = "Learningrange")
    })
    @GetMapping("/selectPage")
    public Result selectPage(Learningrange learningrange,
                             @RequestParam(defaultValue = "1") Integer pageNum,
                             @RequestParam(defaultValue = "10") Integer pageSize) {
        PageInfo<Learningrange> pageInfo = learningrangeService.selectPage(learningrange, pageNum, pageSize);
        return Result.success(pageInfo);
    }
}
